import collections


class Solution(object):
    def movesToChessboard(self, board):
        row_dict = collections.defaultdict(int)
        col_dict = collections.defaultdict(int)
        m = len(board)
        n = len(board[0])
        for i in range(m):
            row_dict[tuple(board[i])] += 1
        for j in range(n):
            col_dict[tuple([board[i][j] for i in range(m)])] += 1
        ans = 0
        if m >= 2:
            key_list = list(row_dict.keys())
            if len(key_list) == 2:
                t = 0
                for j in range(n):
                    if 1 - key_list[0][j] == key_list[1][j]:
                        t += 1
                if t == n:
                    if m % 2 == 0:
                        if row_dict[key_list[0]] == row_dict[key_list[1]]:
                            step1 = 0
                            step2 = 0
                            for i in range(0, m, 2):
                                if tuple(board[i]) == key_list[1]:
                                    step1 += 1
                                if tuple(board[i]) == key_list[0]:
                                    step2 += 1
                            ans += min(step1, step2)
                        else:
                            return -1
                    else:
                        if abs(row_dict[key_list[0]] - row_dict[key_list[1]]) == 1:
                            if row_dict[key_list[0]] < row_dict[key_list[1]]:
                                key_list = [key_list[1], key_list[0]]
                            step = 0
                            for i in range(0, m, 2):
                                if tuple(board[i]) == key_list[1]:
                                    step += 1
                            ans += step
                        else:
                            return -1
                else:
                    return -1
            else:
                return -1
        if n >= 2:
            key_list = list(col_dict.keys())
            if len(key_list) == 2:
                t = 0
                for i in range(m):
                    if 1 - key_list[0][i] == key_list[1][i]:
                        t += 1
                if t == m:
                    if n % 2 == 0:
                        if col_dict[key_list[0]] == col_dict[key_list[1]]:
                            step1 = 0
                            step2 = 0
                            for j in range(0, n, 2):
                                if tuple([board[i][j] for i in range(m)]) == key_list[1]:
                                    step1 += 1
                                if tuple([board[i][j] for i in range(m)]) == key_list[0]:
                                    step2 += 1
                            ans += min(step1, step2)
                        else:
                            return -1
                    else:
                        if abs(col_dict[key_list[0]] - col_dict[key_list[1]]) == 1:
                            if col_dict[key_list[0]] < col_dict[key_list[1]]:
                                key_list = [key_list[1], key_list[0]]
                            step = 0
                            for j in range(0, n, 2):
                                if tuple([board[i][j] for i in range(m)]) == key_list[1]:
                                    step += 1
                            ans += step
                        else:
                            return -1
                else:
                    return -1
            else:
                return -1
        return ans


data = Solution()
board = [[0, 1, 1, 0], [0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1]]
print(data.movesToChessboard(board))
